package com.night.book.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.hck.test.R;
import com.night.book.util.ViewHolderUtil;
import com.night.music.bean.MusicBean;
import com.night.music.ui.PlayMusicActivity;

public class MusicListActivity extends Activity {
	private class MusicAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return mMusicBeans == null ? 0 : mMusicBeans.size();
		}

		@Override
		public Object getItem(int paramInt) {
			return mMusicBeans.get(paramInt);
		}

		@Override
		public long getItemId(int paramInt) {
			return paramInt;
		}

		@Override
		public View getView(int paramInt, View paramView,
				ViewGroup paramViewGroup) {
			if (paramView == null) {
				paramView = ViewHolderUtil.buildView(R.layout.music_item,
						paramViewGroup);
			}
			TextView textView = ViewHolderUtil.findViewById(paramView,
					R.id.music_name);
			String name = mMusicBeans.get(paramInt).getMusicName();
			textView.setText(name);
			return paramView;
		}
	}
	List<MusicBean> mMusicBeans;

	private ListView mMusiclist;

	private void getMusicListData() {
		Cursor cursor = this.getContentResolver().query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
				/* 这个字符串数组表示要查询的列 */
				new String[] { MediaStore.Video.Media.TITLE, // 音乐名
						MediaStore.Audio.Media.DURATION, // 音乐的总时间
						MediaStore.Audio.Media.ARTIST, // 艺术家
						MediaStore.Audio.Media._ID, // id号
						MediaStore.Audio.Media.DISPLAY_NAME, // 音乐文件名
						MediaStore.Audio.Media.DATA // 音乐文件的路径
				}, null,// 查询条件，相当于sql中的where语句
				null, // 查询条件中使用到的数据
				null);// 查询结果的排序方式
		mMusicBeans = new ArrayList<MusicBean>();
		while (cursor != null && cursor.moveToNext()) {
			MusicBean musicBean = new MusicBean();
			musicBean.setId(cursor.getInt(cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media._ID)));
			musicBean
					.setMusicName(cursor.getString(cursor
							.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)));
			musicBean.setMusicPlayUrl(cursor.getString(cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
			// musicBean.setId(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
			mMusicBeans.add(musicBean);
			// LogUtil.d(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
		}
		;
	}

	private void initView() {
		mMusiclist = (ListView) findViewById(R.id.musiclist);
		mMusiclist.setAdapter(new MusicAdapter());
		mMusiclist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> paramAdapterView,
					View paramView, int paramInt, long paramLong) {
				Intent intent = new Intent();
				intent.setClass(MusicListActivity.this, PlayMusicActivity.class);
				intent.putExtra("music", mMusicBeans.toArray());
				intent.putExtra("id", paramInt);
				intent.putExtra("type", "local");
				startActivity(intent);
			}
		});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.music_list);
		getMusicListData();
		initView();
	}
}
